#!/usr/bin/env ruby

#text detection

## @example example7.rb
#  @see example7.c

$LOAD_PATH << File.expand_path("../../../lib/rubyFulguro/fulguro")

require 'fulguro'
require 'flgrrtcapture'
include Fulguro
include Flgrrtcapture


cam0 = FLGR_RtCapture2D.new("/dev/video0")
cam0.set_palette("YUV420P")
cam0.set_size(320,240)
size_x, size_y = cam0.get_size()

imin = FLGR_Data2D.new(size_y,size_x,1,FLGR_UINT8)
immir = FLGR_Data2D.new(size_y,size_x,1,FLGR_UINT8)
imtmp1 = FLGR_Data2D.new(size_y,size_x,1,FLGR_UINT8)
imtmp2 = FLGR_Data2D.new(size_y,size_x,1,FLGR_UINT8)
imout = FLGR_Data2D.new(size_y,size_x,1,FLGR_UINT8)

nhb = FLGR_Data2D.new(9,9,1,FLGR_UINT8,FLGR_RECT,FLGR_8_CONNEX)
nhb2 = FLGR_Data2D.new(1,9,1,FLGR_UINT8,FLGR_RECT,FLGR_8_CONNEX)
nhb3 = FLGR_Data2D.new(9,9,1,FLGR_UINT8,FLGR_RECT,FLGR_8_CONNEX)
nhb4 = FLGR_Data2D.new(9,1,1,FLGR_UINT8,FLGR_RECT,FLGR_8_CONNEX)

scr0 = flgr2d_rtview_create(immir, 1, 2)

for i in 1..200
  flgr2d_rtcapture_gray_image(imin, cam0)
  flgr2d_copy(immir,imin)
  #flgr_mirror_horizontal(immir,imin)
  
  flgr2d_open_tophat(imtmp1,immir,nhb)
  flgr2d_close_tophat(imtmp2,immir,nhb)

  flgr2d_arith_inf(imout,imtmp1,imtmp2)
  flgr2d_replace(imout,imout,'>=',20,255,0)

  flgr2d_close(imtmp1,imout,nhb2)
  flgr2d_open(imout,imtmp1,nhb2)
  
  flgr2d_dilate(imtmp1,imout,nhb3)
  flgr2d_dilate(imout,imtmp1,nhb3)
  flgr2d_dilate(imtmp1,imout,nhb3)
  flgr2d_erode(imout,imtmp1,nhb3)
  flgr2d_erode(imtmp1,imout,nhb3)
  flgr2d_erode(imout,imtmp1,nhb3)

  flgr2d_open(imtmp1,imout,nhb4)

  flgr2d_replace(imout,imtmp1,'==',255,immir,0)
  
  flgr2d_rtview_refresh(scr0, immir,  0, 0)
  flgr2d_rtview_refresh(scr0, imout, 0, 1)
end 

